package com.small.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.small.R;
import com.small.commom.AsyncImageLoader;
import com.small.model.FavDiscountModel;

public class FavDiscountAdapter extends BaseAdapter{

	List<FavDiscountModel> list; 
	LayoutInflater li;
	boolean isEdit = false;
	private Animation animation1;
	private Context ctx;
	
	public FavDiscountAdapter(Context co,List<FavDiscountModel> _list){
		ctx = co;
		this.li=LayoutInflater.from(co);
		list=_list;
		animation1 = AnimationUtils.loadAnimation(ctx, R.anim.shake_button_anim);
		
	}
	
	public void setEdit(boolean b){
		isEdit = b;
		notifyDataSetChanged();
	}
	public boolean getEdit(){ 
		return isEdit;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) { 
		
		FavDiscountModel  item= list.get(position);
		 
		ViewHolder holder=null; 
			
			if(null==convertView){
				holder =new ViewHolder();  
				convertView=li.inflate(R.layout.activity_favdiscount_list_item, null);
				holder.tv_title = (TextView) convertView.findViewById(R.id.item_name);     
		        holder.tv_content = (TextView) convertView.findViewById(R.id.item_content); 
		        holder.img_title = (ImageView) convertView.findViewById(R.id.img_title);
		        holder.img_del = (ImageView) convertView.findViewById(R.id.btn_del); 
		        convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			} 
			 
			if (isEdit) {				 
				 holder.img_del.setBackgroundResource(R.drawable.btn_del);
				 holder.img_del.setAnimation(animation1);
				 animation1.start();
			}else{
				 holder.img_del.clearAnimation();
				 holder.img_del.setBackgroundResource(R.drawable.triangle); 
			}
			
			holder.tv_title.setText(item.getCouponTitle());
			holder.tv_content.setText(item.getCouponDesc());
			System.out.println(item.getCouponID());
			AsyncImageLoader.getInstance().loadImg(item.getCouponID(),item.getSmallImageUrl(),holder.img_title);
			  
		
		return convertView; 		
		
	}
	
	private  class ViewHolder {    
    	public TextView tv_title;
    	public TextView tv_content;
    	public ImageView img_title;
    	public ImageView img_del;
    	 
	}

}
